﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Media;

namespace teach
{
    public partial class AudioChoicesPanel : PanelBase
    {
        private string m_audioPath = null;

        public AudioChoicesPanel()
        {
            InitializeComponent();
        }

        private float GetFont(string data)
        {
            float size = 36;
            if (data.Length > 100)
            {
                size = 20;
            }
            else if (data.Length > 75
                )
            {
                size = 22;
            }
            else if (data.Length > 45)
            {
                size = 24;
            }
            else if (data.Length > 20)
            {
                size = 32;
            }
            return size;
        }

        private float GetButtonFont(string data)
        {
            float size = 21;
            if (data.Length > 20)
            {
                size = 20;
            }
            else if (data.Length > 12)
            {
                size = 20;
            }
            return size;
        }

        /// <summary>
        /// Set the operation question running
        /// </summary>
        /// <param name="question"></param>
        public override void SetQuestion(Question question)
        {
            Reset();
            m_answered = false;
            m_currentQuestion = question;
  
            m_audioPath = Path.Combine(m_additionalResourcePath, question.Properties["audio"]);

            SetButtonModelForThree(question.Properties["answerA"], question.Properties["answerB"], question.Properties["answerC"]);
            btnAnswer1.Font = new System.Drawing.Font("Microsoft Sans Serif", GetButtonFont(btnAnswer1.Text), System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            btnAnswer2.Font = new System.Drawing.Font("Microsoft Sans Serif", GetButtonFont(btnAnswer2.Text), System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            btnAnswer3.Font = new System.Drawing.Font("Microsoft Sans Serif", GetButtonFont(btnAnswer3.Text), System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));

            this.timeoutMeter.Interval = question.Timeout;
            this.timeoutMeter.Enabled = true;
        }

        public override Control GetAnswerControl()
        {
            return btnAnswer1;
        }

        private void SetButtonModelForThree(string text1, string text2, string text3)
        {
            int rndNum = TeachController.RND.Next(6);
            switch (rndNum)
            {
                case 0:
                    btnAnswer1.Text = text1;
                    btnAnswer2.Text = text2;
                    btnAnswer3.Text = text3;
                    break;
                case 1:
                    btnAnswer1.Text = text1;
                    btnAnswer2.Text = text3;
                    btnAnswer3.Text = text2;
                    break;
                case 2:
                    btnAnswer1.Text = text2;
                    btnAnswer2.Text = text1;
                    btnAnswer3.Text = text3;
                    break;
                case 3:
                    btnAnswer1.Text = text2;
                    btnAnswer2.Text = text3;
                    btnAnswer3.Text = text1;
                    break;
                case 4:
                    btnAnswer1.Text = text3;
                    btnAnswer2.Text = text1;
                    btnAnswer3.Text = text2;
                    break;
                case 5:
                    btnAnswer1.Text = text3;
                    btnAnswer2.Text = text2;
                    btnAnswer3.Text = text1;
                    break;
            }
        }

        public override void SetStatusImage(bool success, Image image)
        {
            pctStatus.Image = image;
            pctStatus.Refresh();
            Color col = Color.Blue;
            if (!success)
            {
                col = Color.Red;
            }

            if (btnAnswer1.Text == m_currentQuestion.Answer.ToString())
            {
                btnAnswer1.ForeColor = col;
            }
            else if (btnAnswer2.Text == m_currentQuestion.Answer.ToString())
            {
                btnAnswer2.ForeColor = col;
            }
            else if (btnAnswer3.Text == m_currentQuestion.Answer.ToString())
            {
                btnAnswer3.ForeColor = col;
            }
        }

        private void Reset()
        {
            pctStatus.Image = null;
            pctStatus.Refresh();
            btnNext.Visible = false;
            btnAnswer1.ForeColor = Color.Black;
            btnAnswer2.ForeColor = Color.Black;
            btnAnswer3.ForeColor = Color.Black;
        }

        private void Boutton_Click(object sender, EventArgs e)
        {
            Button btn = sender as Button;
            this.timeoutMeter.Enabled = false;
            if (!m_answered)
            {
                m_answered = true;
                base.RaiseAnswer(btn.Text);
                btnNext.Visible = true;
            }
        }

        private void btnAudio_Click(object sender, EventArgs e)
        {
            SoundPlayer simpleSound = new SoundPlayer(m_audioPath);
            simpleSound.Play();
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            m_answered = false;
            Reset();
            base.RaiseNextClicked();
            btnNext.Visible = false;
        }

    }
}
